package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.exception.BaseException;
import com.hmall.item.constants.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDateTime;
import java.time.ZoneId;


@RestController
@RequestMapping("item")
@Slf4j
public class ItemController {

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/list")
    public PageDTO<Item> queryPage(Integer page, Integer size) {
        //IPage<Item> itemIPage = itemService.queryPage(page,size);

        Page<Item> itemPage = new Page<>(page, size);
        Page<Item> itemPager = itemService.page(itemPage);

        //IPage<Item> itemIPage = itemService.queryPage(page, size);


        return new PageDTO<Item>(itemPager.getTotal(), itemPager.getRecords());
    }

    @GetMapping("/{id}")
    public Item queryItemById(@PathVariable Long id) {
        Item item = itemService.getById(id);

        return item;
    }

    @PostMapping()
    public void addItem(@RequestBody Item item) {

        item.setCommentCount(0);
        item.setSold(0);
        item.setStatus(2);
        log.info("新增商品数据：{}", item);

        item.setCreateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        item.setUpdateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

        itemService.save(item);

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_KEY, item.getId());
    }

    @PutMapping("/status/{id}/{status}")
    @Transactional
    public void editItemStatus(@PathVariable Long id, @PathVariable Integer status) {
        log.info("要 上/下 架的商品id：{},{}", id, status);
        Item item = itemService.getById(id);

        item.setStatus(status);
        item.setUpdateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

        itemService.updateById(item);
        //商品状态 1-正常，2-下架
        String key = status == 2 ? MqConstants.ITEM_DELETE_KEY : MqConstants.ITEM_INSERT_KEY;

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, key , id);
    }

    @PutMapping()
    public void editItem(@RequestBody Item item) {
        log.info("修改后的商品数据：{}", item);

        if (item.getStatus() != 2) {
            throw new BaseException("商品未下架,不可修改！");
        }

        item.setUpdateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        itemService.updateById(item);

        //rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_DELETE_KEY, item.getId());
        //rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_KEY, item.getId());
    }

    @DeleteMapping("/{id}")
    public void deleteItem(@PathVariable Long id) {
        log.info("要删除的商品id：{}", id);
        Item item = itemService.getById(id);

        if (item.getStatus() != 2) {
            throw new BaseException("商品未下架,不可删除！");
        }

        //逻辑删除
        item.setUpdateTime(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
        itemService.removeById(id);

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_DELETE_KEY, item.getId());
    }

}
